package com.tms.warehouse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class ShelfController extends BaseController {

	private static final String PARAMETER_ID = "entityId";
	private static final String PARAMETER_NAME = "name";
	private static final String PARAMETER_NUMBER = "number";
	private static final String PARAMETER_RACK = "rackId";

	@Override
	protected String doListAction(HttpServletRequest req,
			HttpServletResponse resp) {

		req.setAttribute("entities", Warehouse.getAllShelves());
		return "./shelf.jsp";
	}

	@Override
	protected String doCreateAction(HttpServletRequest req,
			HttpServletResponse resp) {

		req.setAttribute("racks", Warehouse.getAllRacks());
		return "./shelf.jsp";
	}

	@Override
	protected String doEditAction(HttpServletRequest req,
			HttpServletResponse resp) {

		String entityId = req.getParameter(PARAMETER_ID);
		req.setAttribute("entity", Warehouse.findShelfById(entityId));
		req.setAttribute("racks", Warehouse.getAllRacks());
		return "./shelf.jsp";
	}

	@Override
	protected String doDeleteAction(HttpServletRequest req,
			HttpServletResponse resp) {

		String entityId = req.getParameter(PARAMETER_ID);
		Warehouse.deleteShelf(entityId);
		return TARGET_SELF;
	}
 
	@Override
	protected String doSaveAction(HttpServletRequest req,
			HttpServletResponse resp) {

		String entityId = req.getParameter(PARAMETER_ID);
		String name = req.getParameter(PARAMETER_NAME);
		String number = req.getParameter(PARAMETER_NUMBER);
		String rackId = req.getParameter(PARAMETER_RACK);
		Warehouse.persistShelf(entityId, name, Integer.parseInt(number), rackId);
		return TARGET_SELF;
	}
}
